package com.spzx.order.receiver;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rabbitmq.client.Channel;
import com.spzx.common.core.constant.MqConstants;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.service.IOrderInfoService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;

import static org.springframework.amqp.rabbit.annotation.Exchange.TRUE;


/**
 * Package: com.spzx.product.receiver
 *
 * @author xiongkunYang
 * @description:
 * @since 2025/6/8 19:07
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class OrderReceiver {

    private final IOrderInfoService orderInfoService;

    /**
     * 取消订单
     *
     * @param orderNo 订单编号
     * @param message 消息
     * @param channel 通道
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(
                    value = MqConstants.EXCHANGE_CANCEL_ORDER,
                    delayed = TRUE,
                    arguments = {@Argument(name = "x-delayed-type", value = "direct")}
            ),
            value = @Queue(value = MqConstants.QUEUE_CANCEL_ORDER, durable = TRUE),
            key = {MqConstants.ROUTING_CANCEL_ORDER}
    ))
    public void delayCancelOrder(String orderNo, Message message, Channel channel) {
        Assert.notNull(orderNo, "订单编号不能为空");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        OrderInfo orderInfo = this.orderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderNo, orderNo));
        if (orderInfo.getOrderStatus() != 0) {
            return;
        }
        try {
            log.info("订单编号：{}，关闭订单成功", orderNo);
            this.orderInfoService.cancelOrder(orderInfo, "超时关闭订单");
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            log.error("关闭订单失败，订单编号：{}", orderNo, e);
            channel.basicNack(deliveryTag, false, false);
        }
    }

    /**
     * 监听订单支付成功消息；更新订单状态
     *
     * @param orderNo  订单编号
     * @param message  消息
     * @param channel  信道
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.EXCHANGE_PAYMENT_PAY),
            value = @Queue(value = MqConstants.QUEUE_PAYMENT_PAY, durable = TRUE),
            key = MqConstants.ROUTING_PAYMENT_PAY
    ))
    public void processPaySuccess(String orderNo, Message message, Channel channel) {
        Assert.notNull(orderNo, "订单编号不能为空");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("订单编号：{}, 修改订单状态...", orderNo);
            orderInfoService.processPaySuccess(orderNo);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error("订单编号：{}, 修改订单状态失败", orderNo, e);
            channel.basicNack(deliveryTag, false, false);
        }
    }

}